﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Outcoder. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2010-09-21 17:24:28Z</CreationDate>
</File>
*/
#endregion
//
//using System;
//using System.ComponentModel;
//using System.Linq;
//using System.Windows;
//using System.Windows.Controls;
//using System.Windows.Data;
//
//namespace Outcoder.ComponentModel
//{
//	public static class PropertyValidation
//	{
//		#region ValidateProperty
//		public static readonly DependencyProperty ValidateProperty
//			= DependencyProperty.RegisterAttached(
//				"Validate",
//				typeof(string),
//				typeof(PropertyValidation),
//				new PropertyMetadata(OnValidatePropertyChanged));
//
//		public static void SetValidate(Control obj, string value)
//		{
//			obj.SetValue(ValidateProperty, value);
//		}
//
//		public static string GetValidate(Control obj)
//		{
//			return (string)obj.GetValue(ValidateProperty);
//		}
//		#endregion
//
//		#region ValidationBehaviorProperty
//		static readonly DependencyProperty ValidationBehaviorProperty
//			= DependencyProperty.RegisterAttached(
//				"ValidationBehavior",
//				typeof(ValidationBehavior),
//				typeof(PropertyValidation),
//				null);
//
//		public static void SetValidationBehavior(Control obj, ValidationBehavior value)
//		{
//			obj.SetValue(ValidationBehaviorProperty, value);
//		}
//
//		public static ValidationBehavior GetValidationBehavior(Control obj)
//		{
//			return (ValidationBehavior)obj.GetValue(ValidationBehaviorProperty);
//		}
//		#endregion
//
//		static void OnValidatePropertyChanged(
//			DependencyObject d, DependencyPropertyChangedEventArgs e)
//		{
//			Control control = (Control)d;
//			DataContextChangedListener.Unsubscribe(control, OnDataContextChanged);
//			DataContextChangedListener.Subscribe(control, OnDataContextChanged);
//		}
//
//		static ValidationBehavior GetOrCreateValidationBehavior(Control d)
//		{
//			var behaviour = GetValidationBehavior(d);
//			if (behaviour == null)
//			{
//				behaviour = new ValidationBehavior(d);
//				SetValidationBehavior(d, behaviour);
//			}
//			return behaviour;
//		}
//
//		static void OnDataContextChanged(
//			DependencyObject d, DependencyPropertyChangedEventArgs e)
//		{
//			Control control = (Control)d;
//			ValidationBehavior behavior = GetOrCreateValidationBehavior(control);
//
//			INotifyDataErrorInfo oldInfo = e.OldValue as INotifyDataErrorInfo;
//			if (oldInfo != null)
//			{
//				behavior.Unsubscribe(oldInfo);
//			}
//
//			INotifyDataErrorInfo newInfo = e.NewValue as INotifyDataErrorInfo;
//			if (newInfo != null)
//			{
//				behavior.Unsubscribe(newInfo);
//				behavior.Subscribe(newInfo);
//			}
//		}
//
//		public class ValidationBehavior
//		{
//			WeakReference owner;
//
//			public Control Owner
//			{
//				get
//				{
//					return owner != null ? (Control)owner.Target : null;
//				}
//				private set
//				{
//					owner = new WeakReference(value);
//				}
//			}
//
//			public ValidationBehavior(Control owner)
//			{
//				Owner = ArgumentValidator.AssertNotNull(owner, "owner");
//			}
//
//			public void Subscribe(INotifyDataErrorInfo errorNotifier)
//			{
//				ArgumentValidator.AssertNotNull(errorNotifier, "errorNotifier");
//				errorNotifier.ErrorsChanged += OnErrorsChanged;
//			}
//
//			public void Unsubscribe(INotifyDataErrorInfo errorNotifier)
//			{
//				ArgumentValidator.AssertNotNull(errorNotifier, "errorNotifier");
//				errorNotifier.ErrorsChanged -= OnErrorsChanged;
//			}
//
//			void OnErrorsChanged(object sender, DataErrorsChangedEventArgs args)
//			{
//				var control = Owner;
//				if (control == null)
//				{
//					Unsubscribe((INotifyDataErrorInfo)sender);
//					return;
//				}
//
//				string propertyName = null;
//				var textBox = control as TextBox;
//				if (textBox != null)
//				{
//					BindingExpression expression
//						= textBox.GetBindingExpression(TextBox.TextProperty);
//					if (expression != null
//						&& expression.ParentBinding != null
//						&& expression.ParentBinding.Path != null)
//					{
//						propertyName = expression.ParentBinding.Path.Path;
//					}
//				}
//
//				if (string.IsNullOrEmpty(propertyName))
//				{
//					propertyName
//						= (string)control.GetValue(ValidateProperty);
//				}
//
//				INotifyDataErrorInfo info = (INotifyDataErrorInfo)sender;
//				var errors = info.GetErrors(propertyName);
//				bool hasErrors = errors != null
//								 && errors.Cast<object>().Any();
//
//				VisualStateManager.GoToState(control,
//						   hasErrors ? "InvalidUnfocused" : "Valid", true);
//			}
//		}
//	}
//}